//
//  MPLoginController.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/30.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "MPLoginController.h"
#import "ATShareTool.h"
#import "MPBindPhoneContoller.h"
#import "ATImageTopButton.h"
#import "MPPrivacyController.h"

@interface MPLoginController ()
{}
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIImageView *logoView;
@property (nonatomic, strong) ATMargingButton *wechatBtn;
@property (nonatomic, strong) ATHightlightButton *checkBtn;
@end

@implementation MPLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
    
    if (![ATShareTool isWXAppInstalled]) {
        [self.wechatBtn setTitle:@"手机验证码登录" forState:UIControlStateNormal];
        [self.wechatBtn removeAllTargets];
        [self.wechatBtn addTarget:self action:@selector(didClickPhoneLogin:) forControlEvents:UIControlEventTouchUpInside];
        [self.wechatBtn setImage:nil forState:UIControlStateNormal];
    }
}

- (void)didClickPhoneLogin:(id)sender {
    [self.navigationController pushViewController:[MPBindPhoneContoller vcWithUserId:nil isEnterprise:self.isEnterprise] animated:YES];
}

- (void)didClickLogin {
    [MBProgressHUD showWithCompletion:^(MBProgressHUD *hud) {
        [ATShareTool sendWechatLogin:^(ATShareResponse status, NSString *code) {
            NSLog(@"微信登陆: %@", code);
            if (status != ATShareResponseSuccess) {
                [hud hideAnimated:YES];
                return;
            }
            [MPHttpTool POST:[NSString stringWithFormat:@"business/wxLogin/%@", code] params:nil success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                [hud hideWithSuccess:@"登录成功" completion:^{
                    ATUserModel *user = [ATUserModel modelWithJSON:result.data];
                    if (user.isLogin) {
                        [ATLocal loginWithUser:user];
                        [ATLocal setIsEnterprise:self.isEnterprise];
                        [self dismissOrPopToRootControlelr];
                        return;
                    }
                    [self.navigationController pushViewController:[MPBindPhoneContoller vcWithUserId:user.userId isEnterprise:self.isEnterprise] animated:YES];
                }];
            } failure:ATHttpFailureWithHud(hud)];
        }];
    }];
}

- (BOOL)isEnterprise {
    return self.segment.selectedSegmentIndex == 1;
}
- (void)didClickCheck {
    self.checkBtn.selected = !self.checkBtn.isSelected;
    self.wechatBtn.enabled = self.checkBtn.isSelected;
}

- (void)didClickAgreement:(id)sender {
    [self.navigationController presentViewController:[[BaseNavigationController alloc] initWithRootViewController:[MPPrivacyController new]] animated:YES completion:nil];
}
#pragma mark - setupViews
- (void)setupViews {
    self.at_prefersNavigationBarHidden = YES;
    
    self.logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_logo"]];
    [self.view addSubview:self.logoView];
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoView.superview).offset(SCALEW(56) + NAVI_HIGHT);
        make.centerX.equalTo(self.logoView.superview);
    }];
    
    [self.view addSubview:self.wechatBtn];
    [self.wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.wechatBtn.superview);
        make.height.equalTo(@(44));
        make.width.equalTo(@(SCALEW(258)));
    }];
    
    [self.view addSubview:self.segment];
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.segment.superview);
        make.height.equalTo(@(44));
        make.width.equalTo(@(SCALEW(258)));
        make.top.equalTo(self.wechatBtn.mas_bottom).offset(28);
    }];
    
    [self.view addSubview:self.checkBtn];
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.segment.mas_left);
        make.top.equalTo(self.segment.mas_bottom).offset(20);
        make.bottom.equalTo(self.checkBtn.superview).offset(-SCALEW(186) - TAB_BAR_ADDING);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14.0f];
    label.textColor = [UIColor colorWithRGB:0x999999];
    label.text = @"您已同意";
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.checkBtn);
        make.left.equalTo(self.checkBtn.mas_right).offset(4);
    }];
    
    ATHightlightButton *agreement = [ATHightlightButton buttonWithTitle:@"隐私条款" color:[UIColor appColor] target:self action:@selector(didClickAgreement:)];
    agreement.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [self.view addSubview:agreement];
    [agreement mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.left.equalTo(label.mas_right).offset(4);
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - getter
- (UISegmentedControl *)segment {
    if (!_segment) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[@"个人版", @"企业版"]];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor appColor], NSFontAttributeName : [UIFont systemFontOfSize:18.0f]} forState:UIControlStateNormal];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:18.0f]} forState:UIControlStateSelected];
        [_segment setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [_segment setBackgroundImage:[UIImage imageWithColor:[UIColor appColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        [_segment setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRGB:0xF3F3F3]] forState:UIControlStateNormal|UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        [_segment setBackgroundImage:[UIImage imageWithColor:[UIColor appColorHighlightd]] forState:UIControlStateSelected|UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        _segment.selectedSegmentIndex = [ATLocal isEnterprise] ? 1 : 0;
        _segment.tintColor = [UIColor appColor];
        _segment.clipsToBounds = YES;
        _segment.layer.cornerRadius = 22.0f;
        _segment.layer.borderWidth = 1.0f;
        _segment.layer.borderColor = [UIColor appColor].CGColor;
    }
    return _segment;
}
- (ATMargingButton *)wechatBtn {
    if (!_wechatBtn) {
        _wechatBtn = [ATMargingButton buttonWithImage:[UIImage imageNamed:@"login_wechat"] title:@"微信授权登录" color:[UIColor whiteColor] target:self action:@selector(didClickLogin)];
        _wechatBtn.clipsToBounds = YES;
        _wechatBtn.layer.cornerRadius = 22.0f;
        _wechatBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        _wechatBtn.imageMarning = 9.0f;
        [_wechatBtn setBackgroundImage:[UIImage imageWithColor:[UIColor appColor]] forState:UIControlStateNormal];
        [_wechatBtn setBackgroundImage:[UIImage imageWithColor:[UIColor appColorHighlightd]] forState:UIControlStateHighlighted];
        [_wechatBtn setBackgroundImage:[UIImage imageWithColor:[UIColor appColorDisable]] forState:UIControlStateDisabled];
    }
    return _wechatBtn;
}
- (ATHightlightButton *)checkBtn {
    if (!_checkBtn) {
        _checkBtn = [ATHightlightButton buttonWithImage:[UIImage imageNamed:@"login_CheckBox_nor"] target:self action:@selector(didClickCheck)];
        [_checkBtn setImage:[UIImage imageNamed:@"login_CheckBox_sel"] forState:UIControlStateSelected];
        _checkBtn.selected = YES;
    }
    return _checkBtn;
}
@end
